home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #3 / Amiga Plus CD - 2002 - No. 03.iso / AmiSoft / Dev / C / Tinygl.lha / TinyGL / examples / texobj.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-18  |  3.9 KB  |  194 lines

  1. /*
  2. * Example of using the 1.1 texture object functions.
  3. * Also, this demo utilizes Mesa's fast texture map path.
  4. *
  5. * Brian Paul   June 1996
  6. */
  7.  
  8. #include <math.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #include <GL/glut.h>
  14.  
  15. static GLuint TexObj[2];
  16. static GLfloat Angle = 0.0f;
  17.  
  18. static int cnt=0,v=0;
  19. int w=320,h=240;
  20.  
  21. void draw(void) {
  22.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  23.  
  24.     glColor3f(1.0, 1.0, 1.0);
  25.  
  26.     /* draw first polygon */
  27.     glPushMatrix();
  28.     glTranslatef(-1.0, 0.0, 0.0);
  29.     glRotatef(Angle, 0.0, 0.0, 1.0);
  30.     glBindTexture(GL_TEXTURE_2D, TexObj[v]);
  31.  
  32.     glEnable(GL_TEXTURE_2D);
  33.     glBegin(GL_QUADS);
  34.     glTexCoord2f(0.0, 0.0);
  35.     glVertex2f(-1.0, -1.0);
  36.     glTexCoord2f(1.0, 0.0);
  37.     glVertex2f(1.0, -1.0);
  38.     glTexCoord2f(1.0, 1.0);
  39.     glVertex2f(1.0, 1.0);
  40.     glTexCoord2f(0.0, 1.0);
  41.     glVertex2f(-1.0, 1.0);
  42.     glEnd();
  43.     glDisable(GL_TEXTURE_2D);
  44.     glPopMatrix();
  45.  
  46.     /* draw second polygon */
  47.     glPushMatrix();
  48.     glTranslatef(1.0, 0.0, 0.0);
  49.     glRotatef(Angle - 90.0, 0.0, 1.0, 0.0);
  50.  
  51.     glBindTexture(GL_TEXTURE_2D, TexObj[1-v]);
  52.  
  53.     glEnable(GL_TEXTURE_2D);
  54.     glBegin(GL_QUADS);
  55.     glTexCoord2f(0.0, 0.0);
  56.     glVertex2f(-1.0, -1.0);
  57.     glTexCoord2f(1.0, 0.0);
  58.     glVertex2f(1.0, -1.0);
  59.     glTexCoord2f(1.0, 1.0);
  60.     glVertex2f(1.0, 1.0);
  61.     glTexCoord2f(0.0, 1.0);
  62.     glVertex2f(-1.0, 1.0);
  63.     glEnd();
  64.     glDisable(GL_TEXTURE_2D);
  65.  
  66.     glPopMatrix();
  67.     
  68.     glutSwapBuffers();
  69. }
  70.  
  71.  
  72. /* new window size or exposure */
  73. void reshape(int width, int height) {
  74.     glViewport(0, 0, (GLint) width, (GLint) height);
  75.     glMatrixMode(GL_PROJECTION);
  76.     glLoadIdentity();
  77.     glFrustum(-2.0, 2.0, -2.0, 2.0, 6.0, 20.0);
  78.     glMatrixMode(GL_MODELVIEW);
  79.     glLoadIdentity();
  80.     glTranslatef(0.0, 0.0, -8.0);
  81. }
  82.  
  83.  
  84. void bind_texture(int texobj,int image)
  85. {
  86.     static int width = 8, height = 8;
  87.     static int color[2][3]={
  88.         {255,0,0},
  89.         {0,255,0},
  90.     };
  91.     GLubyte tex[64][3];
  92.     static GLubyte texchar[2][8*8] = {
  93.         {
  94.             0, 0, 0, 0, 0, 0, 0, 0,
  95.                 0, 0, 0, 0, 1, 0, 0, 0,
  96.                 0, 0, 0, 1, 1, 0, 0, 0,
  97.                 0, 0, 0, 0, 1, 0, 0, 0,
  98.                 0, 0, 0, 0, 1, 0, 0, 0,
  99.                 0, 0, 0, 0, 1, 0, 0, 0,
  100.                 0, 0, 0, 1, 1, 1, 0, 0,
  101.                 0, 0, 0, 0, 0, 0, 0, 0},
  102.             {
  103.                 0, 0, 0, 0, 0, 0, 0, 0,
  104.                     0, 0, 0, 2, 2, 0, 0, 0,
  105.                     0, 0, 2, 0, 0, 2, 0, 0,
  106.                     0, 0, 0, 0, 0, 2, 0, 0,
  107.                     0, 0, 0, 0, 2, 0, 0, 0,
  108.                     0, 0, 0, 2, 0, 0, 0, 0,
  109.                     0, 0, 2, 2, 2, 2, 0, 0,
  110.                     0, 0, 0, 0, 0, 0, 0, 0}};
  111.  
  112.                 int i,j;
  113.  
  114.                 glBindTexture(GL_TEXTURE_2D, texobj);
  115.  
  116.                 /* red on white */
  117.                 for (i = 0; i < height; i++) {
  118.                     for (j = 0; j < width; j++) {
  119.                         int p = i * width + j;
  120.                         if (texchar[image][(height - i - 1) * width + j]) {
  121.                             tex[p][0] = color[image][0];
  122.                             tex[p][1] = color[image][1];
  123.                             tex[p][2] = color[image][2];
  124.                         } else {
  125.                             tex[p][0] = 255;
  126.                             tex[p][1] = 255;
  127.                             tex[p][2] = 255;
  128.                         }
  129.                     }
  130.                 }
  131.                 glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0,
  132.                     GL_RGB, GL_UNSIGNED_BYTE, tex);
  133.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  134.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  135.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  136.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  137.                 /* end of texture object */
  138. }
  139.  
  140.  
  141.  
  142. void init(void) {
  143.     glEnable(GL_DEPTH_TEST);
  144.  
  145.     /* Setup texturing */
  146.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  147.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  148.  
  149.     /* generate texture object IDs */
  150.     glGenTextures(2, TexObj);
  151.     bind_texture(TexObj[0],0);
  152.     bind_texture(TexObj[1],1);
  153. }
  154.  
  155.  
  156. void idle(void) {
  157.     Angle += 2.0;
  158.     if (++cnt==50) {
  159.         cnt=0;
  160.     //    v=!v;
  161.     }
  162.     draw();
  163.     glutPostRedisplay();
  164. }
  165.  
  166. /* change view angle, exit upon ESC */
  167. GLenum key(int k, GLenum mask)
  168. {
  169.     switch (k) {
  170.     case 'q':
  171.     case 27:
  172.         glutDestroyWindow(0);
  173.         exit(0);
  174.     }
  175.     return GL_FALSE;
  176. }
  177.  
  178. int main(int argc, char **argv)    {
  179.     glutInit(&argc, argv);
  180.     glutInitWindowSize(w, h);
  181.     glutInitWindowPosition(0, 0);
  182.  
  183.     glutCreateWindow("Texture-mapped object");
  184.     init();
  185.  
  186.     glutDisplayFunc(draw);
  187.     glutReshapeFunc(reshape);
  188.     glutIdleFunc(idle);
  189.     glutMainLoop();
  190.  
  191.     return 0;
  192. }
  193.  
  194.